<?php

namespace backend\controllers;

use backend\models\form\ResetPasswordForm;
use backend\service\AuthService;
use backend\service\StoreService;
use common\service\UtilService;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;

/**
 * Site controller
 */
class SiteController extends Controller {

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex() {
        if (Yii::$app->admin->isGuest) {
            return $this->redirect(["login/index"]);
        } else {
            $username = Yii::$app->admin->identity->username ;
            $user_id = Yii::$app->admin->identity->user_id ;
            $url = AuthService::getUserLoginUrl($username,$user_id) ;
            if($url=="/site/index"){
                return $this->render("index") ;
            }else{
                $url = Url::to([$url]) ;
                return $this->redirect($url) ;
            }
        }
    }

    //重置密码
    public function actionResetPassword(){
        if (Yii::$app->admin->isGuest) {
            return $this->redirect(["login/index"]);
        } else {
            $model = new ResetPasswordForm() ;
            $use_id = Yii::$app->admin->identity->user_id ;
            $post_data = Yii::$app->request->post() ;
            if($model->load($post_data) && $model->validate()){
                $add_rs = $model->retPassword($use_id) ;
                return $this->redirect(["tip/index","msg"=>$add_rs['msg'],"url"=>Url::toRoute(["/site/index"])]) ;
            }
            return $this->render('reset_pwd',["model"=>$model,"user_id"=>$use_id]);
        }
    }

    /**
     * 获取门店信息
     * @return array
     */
    public function actionStoreInfo(){
        Yii::$app->response->format = Response::FORMAT_JSON;
        if (Yii::$app->admin->isGuest) {
            return ["code"=>400,"msg"=>"请登陆"];
        }
        $storeCode = Yii::$app->request->post("store_code","");
        if(empty($storeCode)){
            return ["code"=>400,"msg"=>"请不乱操作"];
        }
        $stores = StoreService::findAllStoreByCode() ;

        if(empty($stores[$storeCode])){
            return ["code"=>400,"msg"=>"非法门店"];
        }

        return ["code"=>200,"msg"=>"成功","data"=>$stores[$storeCode]] ;
    }

    /**
     * 获取地址
     */
    public function actionGetLocation(){
        Yii::$app->response->format = Response::FORMAT_JSON;
        if (Yii::$app->admin->isGuest) {
            return ["code"=>400,"msg"=>"请登陆"];
        }
        $address = Yii::$app->request->post("address","");
        $storeLogitude = Yii::$app->request->post("logitude","");
        $storeLatiude = Yii::$app->request->post("latitude","");

        if(empty($address)){
            return ["code"=>400,"msg"=>"请不乱操作"];
        }

        if(empty($storeLogitude)){
            return ["code"=>400,"msg"=>"经度不能空"];
        }

        if(empty($storeLatiude)){
            return ["code"=>400,"msg"=>"纬度不能空"];
        }

        $address = urldecode($address) ;
        $rs = UtilService::getLocation($address) ;
        if($rs['code']!=200){
            return ["code"=>400,"msg"=>$rs['msg']];
        }

        $logitude = $rs['logitude'] ;
        $latiude  = $rs['latiude'] ;

        $rs = UtilService::getDistance($storeLatiude,$storeLogitude,$latiude,$logitude) ;
        if($rs['code']!=200){
            return ["code"=>400,"msg"=>$rs['msg']];
        }


        $distance = $rs['distance'] ;

        $data = [
            "longitude" => $logitude ,
            "latitude" => $latiude ,
            "distance" => $distance ,
        ] ;
        return ["code"=>200,"msg"=>"成功","data"=>$data];

    }


}
